package com.daxia.bumall.common;

import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.daxia.bumall.dto.UserDTO;
import com.daxia.bumall.service.UserService;

@Component
public class UserSessionManager {

	@Autowired
	private SessionCache sessionCache;

	@Autowired
	private UserService userService;

	@PostConstruct
	public void init() {
		List<UserDTO> list = userService.findAll();
		for (UserDTO userDTO : list) {
			String sessionID = userDTO.getSessionID();
			if (StringUtils.isNotBlank(sessionID)) {
				if (!sessionCache.exists(sessionID)) {
					sessionCache.set(userDTO.getId(), sessionID);
				}
			}
		}
	}
}
